package com.document.common.utils;

import com.document.entity.enums.PermissionEnum;
import com.document.entity.po.UserEntity;
import org.apache.shiro.SecurityUtils;

/**
 * 获取登录用户信息
 * @author zhangwei
 * @date 2019/4/17 15:01
 */
public class AuthUserToken {

    public static UserEntity getUser(){
        UserEntity userEntity = (UserEntity)SecurityUtils.getSubject().getPrincipal();
        if(userEntity == null){
            userEntity = new UserEntity();
            userEntity.setId(00000L);
            userEntity.setUsername("system");
            userEntity.setPermission(PermissionEnum.COMMON.getStatus());
        }
        return userEntity;
    }

    public static boolean isAdmin(){
        Integer permission = getUser().getPermission();
        if(permission == null || permission != PermissionEnum.ADMIN.getStatus() || getUser().getId() != 1L){
            return false;
        }
        return true;
    }

    /**
     * 卧槽 累死了 在写两个方法获取userId和userName
     * @date 2019-12-5 14:31:53
     * @return
     */
    public static Long getUserId(){
        return getUser().getId();
    }

    public static String getUserName(){
        return getUser().getUsername();
    }
}
